#!/usr/bin/perl -w

##HACKABOT_HELP##
# Get a random or specific acronym!
# !wtf [something] | --add acronym something it stands for
##HACKABOT_HELP##

use strict;
use DBI;
use Time::localtime;
use XML::Simple;

my $CONFIG = XMLin($ENV{'HACKABOT_CFG'}); 
my $DBCFG = $CONFIG->{'cmdconfig'}->{'database'};

my $dbhost = $DBCFG->{'host'};
my $dbname = $DBCFG->{'name'};
my $dbuser = $DBCFG->{'user'};
my $dbpass = $DBCFG->{'pass'};

my $privmsg = 0;
my ($chan, $msg, $nick);
while (<>) {
	if (/^type\s+privmsg/) {
		$privmsg = 1;
	}
	elsif (/^to\s+(.+)/) {
		$chan = $1;
	}
	elsif (/^nick\s+(.+)/) {
		$nick = $1;
	}
	elsif (/^msg\s+(\S.+)/) {
		$msg = $1;
	}
}

if (not defined $nick or not defined $chan) {
	die "wtf: not all info was given.\n";
}

if (defined $msg and $msg =~ /^--add\s+(\S+)\s+(\S.*)/) {
	my $wtf = $1;
	my $text = $2;
	chomp $text;
	my $ret;
	if ($privmsg) {
		$ret = add_wtf($wtf, $text, $nick);
	}
	else {
		$ret = add_wtf($wtf, $text, $nick, $chan);
	}
	print "send $nick: $ret\n";
	exit;
}

my $wtf;
if (defined $msg) {
	$msg =~ m/(\S+)/;
	$wtf = $1;
}
my $answer = get_wtf($wtf);
print "send $nick: $answer\n";


sub get_wtf {
	my $wtf = shift;
	my ($dbh, $sth);

	$dbh = DBI->connect("DBI:mysql:$dbname:$dbhost", $dbuser, $dbpass, { PrintError => 0 });
	if (!$dbh) {
		die "wtf: Failed to connect to database\n";
	}
	
	my $time = `date "+%y%m%d%H%M"`;
	chomp $time;
	$time = $dbh->quote($time);

	if (not defined $wtf) {
		$sth = $dbh->prepare("SELECT id,acronym,text,lastused FROM wtf ORDER BY RAND()*(1/lastused) DESC LIMIT 1");
	}
	else {
		my $wtfi = $dbh->quote(lc($wtf));
		$sth = $dbh->prepare("SELECT id,acronym_i,acronym,text,lastused FROM wtf WHERE acronym_i = $wtfi");
	}

	$sth->execute;
	my $row = $sth->fetchrow_hashref;
	my $id = $row->{'id'};
	my $value;
	if (defined $row->{'acronym'}) {
		$value = $row->{'acronym'}.": ".$row->{'text'};
	}
	elsif (defined $wtf) {
		$value = "Sorry, I don't know what $wtf means..."
	}
	else {
		$value = "Sorry, I don't know anything... (empty db?)"
	}
	$sth->finish();

	$id = $dbh->quote($id);
	$dbh->do("UPDATE wtf SET lastused = $time WHERE id = $id") 
		or $value .= " and I can't seem to update the wtf database";

	$dbh->disconnect;

	return $value;
}

sub add_wtf {
	my $wtf = shift;
	my $text = shift;
	my $nick = shift;
	my $chan = shift;

	my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost", $dbuser, $dbpass, { PrintError => 0 });
	if (!$dbh) {
		print STDERR "wtf: Failed to connect to database\n";
		return  "I'm a failure! I couldn't add it. :-(";
	}
	
	my $time = `date "+%F %R"`;
	chomp $time;
	$time = $dbh->quote($time);
	
	my $wtfi = $dbh->quote(lc($wtf));
	$wtf = $dbh->quote($wtf);
	$text = $dbh->quote($text);
	$nick = $dbh->quote($nick);
	if (defined $chan) {
		$chan = $dbh->quote($chan);
	}
	else {
		$chan = 'NULL';
	}
	
	my $ret = "Successfully added.";
	$dbh->do("INSERT wtf SET acronym_i = $wtfi, acronym = $wtf, text = $text, nick = $nick, chan = $chan, date = $time") or $ret = "I'm a failure! I couldn't add it. :-(";

	$dbh->disconnect;

	return $ret;
}
